package com.boarsoft.boar.sys.biz;

import java.util.List;

import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.boarsoft.boar.sys.SysParamBiz;
import com.boarsoft.boar.sys.entity.SysParam;
import com.boarsoft.common.Util;
import com.boarsoft.common.dao.PagedResult;
import com.boarsoft.hibernate.biz.SimpleBizImpl;

@Component
public class SysParamBizImpl extends SimpleBizImpl implements SysParamBiz {
	@Override
	@Transactional
	public void update(SysParam o) {
		dao.update(o);
	}

	@Override
	@Transactional(readOnly = true)
	public PagedResult<SysParam> list(String key, String orderBy, int pageNo, int pageSize) {
		StringBuffer sb = new StringBuffer();
		if (Util.strIsNotEmpty(key)) {
			sb.append(" and (code like '%").append(key);
			sb.append("%' or name like '%").append(key);
			sb.append("%' or value like '%").append(key);
			sb.append("%' or type like '%").append(key);
			sb.append("%' or memo like '%").append(key);
			sb.append("%')");
		}
		int total = dao.getTotal(SysParam.class, sb.toString());
		if (Util.strIsNotEmpty(orderBy)) {
			// String ob = Util.array2str(("e." + orderBy).replaceAll(",",
			// ",").split(","), ",");
			sb.append(" order by ").append(orderBy);
		} else {
			sb.append(" order by code");
		}
		List<SysParam> lt = dao.list(SysParam.class, sb.toString(), pageNo, pageSize);
		return new PagedResult<SysParam>(total, lt, pageNo, pageSize);
	}

	@Override
	@Transactional
	public boolean add(SysParam o) {
		SysParam a = dao.get(SysParam.class, o.getCode());
		if (a == null) {
			dao.save(o);
			return true;
		}
		return false;
	}

	@Override
	@Transactional
	public SysParam delete(String code) {
		SysParam o = dao.get(SysParam.class, code);
		dao.delete(o);
		return o;
	}

	@Override
	@Transactional
	public SysParam start(String code) {
		SysParam o = dao.get(SysParam.class, code);
		if (o == null) {
			return o;
		}
		o.setStatus(SysParam.STATUS_INUSE);
		return o;
	}

	@Override
	@Transactional
	public SysParam stop(String code) {
		SysParam o = dao.get(SysParam.class, code);
		if (o == null) {
			return null;
		}
		o.setStatus(SysParam.STATUS_NOUSE);
		return o;
	}

	@Override
	@Transactional(readOnly = true)
	public SysParam get(String code) {
		return dao.get(SysParam.class, code);
	}
}